package com.macaline.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

public class HttpUtil {

	public static final String CHARSET_NAME = "UTF-8";

	public static void main(String[] args) {


		new Thread( () -> {
			for (int i = 0; i < 10; i++) {
				sendRequest("http://127.0.0.1:8100/qdwg/screen/home/apiLog?param=" + UUID.randomUUID(), "GET", "UTF-8");
			}
		}).start();
		new Thread( () -> {
			for (int i = 0; i < 10; i++) {
				sendRequest("http://127.0.0.1:8100/qdwg/screen/home/apiLog?param=" + UUID.randomUUID(), "GET", "UTF-8");
			}
		}).start();
		new Thread( () -> {
			for (int i = 0; i < 10; i++) {
				sendRequest("http://127.0.0.1:8100/qdwg/screen/home/apiLog?param=" + UUID.randomUUID(), "GET", "UTF-8");
			}
		}).start();



	}

	public static String sendRequest(String urlParam,String requestType, String charsetName) {
		HttpURLConnection con = null;
		BufferedReader buffer = null;
		StringBuffer resultBuffer = null;
		try {
			URL url = new URL(urlParam);
			//得到连接对象
			con = (HttpURLConnection) url.openConnection();
			//设置请求类型
			con.setRequestMethod(requestType);
			//设置请求需要返回的数据类型和字符集类型
			con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			con.setRequestProperty("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiIxNTU4ODk5MDAwMiIsInRlbGVwaG9uZSI6IjE1NTg4OTkwMDAyIiwidXNlck5hbWUiOiIxNTU4ODk5MDAwMiIsInVzZXJJZCI6IjJlNzJkMzliYjAxZDQ3MmM4ZDg2ZjRhZGRmODg2NmYxIiwib3JnSWQiOiIwZmRhNDg4YjY1NzM0ZDYzYWVlZDg0ODUxZDY0NzE0NiIsImNsaWVudF9pZCI6ImJkNzk3NGE2ZTg5NzQ2Y2JhMzY0NDYzMzJjMDQ4MzE5IiwiYXVkIjpbIm9hdXRoMi1yZXNvdXJjZSJdLCJjcmVhdGVUaW1lIjoxNjM4NDMyNDM4MDAwLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiLCJ0cnVzdCJdLCJuYW1lIjoi5puy57u06LaFIiwidGVuYW50SWQiOiIiLCJleHAiOjE2ODEyOTIwNTAsImp0aSI6IjhiMzQwNTliLTJiNWUtNGY2OC04ZjBhLTIyMGZmZGM5ZGZkZiJ9.aeTsSriYiHnsE42V0JfeBChB8SKjIGi8BlCjmMKwVCL5_9yMahHiOH3naSivH27HJBUEJnMDqytJM7m7tweHwuzNZAZF3AmSG-f0Cu9riHQ_cPHVOpqpjYXe4dEp9_KQXs6_FWcgMYTm2i9bsKP-nA0g4QIa7gcZsASE3fLBC_XseIfR3GMK7SiYzaqWJDR3c6bbaGTtMa1lVMdLHK3_Y1txZSw0x4tVd5XDajsilqJPYzIFdIeaX-XhvxfzH5aDDbTKuX-GDw-iEAgW7gmclU4QgQDHsLeYiE0UlFAi2ZTZvvPYYOVzHewifI8ikVcfSQQzBwwK8YzvCHFAnEvpZQ");
			//允许写出
			con.setDoOutput(true);
			//允许读入
			con.setDoInput(true);
			//不使用缓存
			con.setUseCaches(false);
			//得到响应码
			int responseCode = con.getResponseCode();
			if(responseCode == HttpURLConnection.HTTP_OK){
				//得到响应流
				InputStream inputStream = con.getInputStream();
				//将响应流转换成字符串
				resultBuffer = new StringBuffer();
				String line;
				buffer = new BufferedReader(new InputStreamReader(inputStream, charsetName));
				while ((line = buffer.readLine()) != null) {
					resultBuffer.append(line);
				}
				return resultBuffer.toString();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
